<template>
  <div class="app-container ml-1">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="分类名称">
        <el-input v-model="categorySearch.classifyName" clearable style="width: 180px" placeholder="分类名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="handleSearch">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="handleAddAndUpdate(1,-1)">新增</el-button>
      </el-form-item>
      <el-form-item>
        <transition name="el-zoom-in-center">
          <el-button v-show="showDel" type="danger" icon="el-icon-delete" @click="delCategory(2,-1)">删除所选</el-button>
        </transition>
      </el-form-item>
    </el-form>
    <el-table
      class="el-table"
      border
      style="width: 98%;"
      :data="categoryList.list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        align="center"
        type="selection"
        width="55"
      />
      <el-table-column
        align="center"
        label="序号"
        type="index"
        width="80"
      />
      <el-table-column
        align="center"
        prop="classifyName"
        label="分类名称"
        width="350"
      />
      <el-table-column
        align="center"
        prop="sort"
        label="排序"
        width="250"
      />
      <el-table-column
        align="center"
        prop="createTime"
        label="创建时间"
        width="350"
      />
      <el-table-column
        align="center"
        label="更新时间"
        width="350"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.updateTime===null" class="no-time">暂无更新时间</span>
          <span v-else class="no-time">{{ scope.row.updateTime }}</span>

        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="操作"
      >
        <template slot-scope="scope" class="box">
          <el-button
            plain
            size="mini"
            @click="handleAddAndUpdate(2,scope.row.id)"
          >修改
          </el-button>
          <el-popconfirm
            class="ml-1"
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除此分类吗？"
            @onConfirm="delCategory(1,scope.row.id)"
          >
            <el-button
              slot="reference"
              type="danger"
              size="mini"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination
      background
      :current-page="categoryList.pageNum"
      :page-sizes="[10, 50, 100, 200]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="categoryList.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @prev-click="handlePrevClick"
      @next-click="handleNextClick"
    />
    <!--    修改/新增弹框-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="category" :model="category"  autocomplete="on" :rules="categoryRules">
        <el-form-item label="分类名称" style="width: 80%" label-width="80px" prop="classifyName"
        >
          <!--          <el-input-->

          <!--            v-model="category.classifyName" placeholder="请输入分类名称"/>-->
          <el-input
            v-model="category.classifyName"
            placeholder="请输入分类名称"
            name="classifyName"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>
        <el-form-item label="分类排序" label-width="80px" >
          <el-input-number v-model="category.sort" :min="1" :max="999" label="分类排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button v-if="submitFlag===1" type="primary" @click="submitAdd">确定添加</el-button>
        <el-button v-if="submitFlag===2" type="primary" @click="submitUpdate">确定修改</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import { getCategory, addCategory, getCategoryInfo, updateCategory, delCategory } from '@/api/category'

export default {
  name: 'Category',
  data() {
    const  validateClassifyName = (rule, value, callback) =>{
      if (value.length < 1) {
        callback(new Error('分类名称不能为空'))
      } else {
        // callback()
      }
    }
    return {
      categorySearch: {
        classifyName: ''
      }, // 用于搜索条件
      category: {
        sort: 1,
        classifyName: '',
      }, // 修改回显 [{ required: true, trigger: 'blur', validator: validateClassifyName }]
      categoryRules:{
        classifyName:[{ required: true, trigger: 'blur', validator: validateClassifyName }]
      },
      pageNum: 1,
      pageSize: 10,
      categoryList: {
        list: []
      },
      dialogVisible: false,
      dialogTitle: '',
      showDel: false,
      submitFlag: 0,
      ids: []

    }
  },
  created() {
    this.getCategory()
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val.length)
      this.ids = []
      if (val.length > 0) {
        val.forEach(res => {
          this.ids.push(res.id)
        })
      }
      this.showDel = val.length > 0
    },
    handleCancel() {
      this.dialogVisible = false
    },
    delCategory(type, id) {
      const parameter = {
        id: []
      }
      if (type === 1) {
        parameter.id.push(id)
      }
      if (type === 2) {
        parameter.id = this.ids
      }

      delCategory(parameter).then(res => {
        if (res.success) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getCategory()
        }
      })
    },
    // 分类添加或修改的弹窗
    handleAddAndUpdate(type, id) {
      this.dialogVisible = true
      // 分类添加
      if (type === 1) {
        this.restCategory()
        this.dialogTitle = '新增分类'
        this.submitFlag = 1
      }
      // 分类修改
      if (type === 2) {
        this.submitFlag = 2
        const parameter = {
          id: id
        }
        this.dialogTitle = '修改分类'
        getCategoryInfo(parameter).then(res => {
          this.category = res.dataMap
          console.log(res)
        })
      }
    },
    submitAdd() {
      addCategory(this.category).then(res => {
        if (res.success) {
          this.dialogVisible = false
          this.getCategory()
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    submitUpdate() {
      updateCategory(this.category).then(res => {
        if (res.success) {
          this.dialogVisible = false
          this.restCategory()
          this.getCategory()
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    // 重置添加参数
    restCategory() {
      this.category = {
        sort: 1,
        classifyName: ''

      }
    },
    // 弹框关闭提示
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getCategory()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getCategory()

      console.log(`当前页: ${val}`)
    },
    handlePrevClick() {
      this.pageNum--
      this.getCategory()
    },
    handleNextClick() {
      this.pageNum++
      this.getCategory()
    },
    handleSearch() {
      this.pageNum =1
      this.pageSize = 10
      this.getCategory()
    },
    getCategory() {
      getCategory(this.categorySearch, this.pageNum, this.pageSize).then(res => {
        this.categoryList = res.dataMap
      })
    }

  }

}
</script>

<style scoped>
.no-time {
  color: #afafaf;
}

.el-pagination {
  float: right;
  margin: 10px 30px;
}

</style>
